package org.sjs.dreamfly.model

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity(tableName = "history_item")
data class HistoryItem(
    @PrimaryKey(autoGenerate = true)
    var id: Long,
    var title: String,
    var resourceUrl: String,
    var referUrl: String,
    var imageUrl: String,
    /**
     * movieId or articleId or videoId
     */
    var resourceId: String,

    @ResourceType
    var resourceType: Int,
    @Ignore
    var movieInfo: MovieInfo? = null,
    @Ignore
    var videoInfo: VideoInfo? = null,
    @Ignore
    var articleInfo: ArticleInfo? = null,
    var videoProgress: Long = 0,
    var articleProgress: Int = 0,
    var movieProgress: Int = 0,
    var modifyTime: Long = System.currentTimeMillis(),
) {
    constructor() : this(
        0,
        "",
        "",
        "",
        "",
        "",
        ResourceType.ARTICLE,
        null,
        null,
        null,
        0,
        0,
        0,
        0,
    )

    constructor(articleInfo: ArticleInfo) : this(
        0,
        articleInfo.title,
        articleInfo.url,
        articleInfo.url,
        "",
        articleInfo.id,
        ResourceType.ARTICLE,
        null,
        null,
        articleInfo,
        0,
        0,
        0,
        0,
    )

    // TODO: 字段不对，待修改
    constructor(videoInfo: VideoInfo) : this(
        0,
        videoInfo.title ?: "",
        videoInfo.videoUrl ?: "",
        videoInfo.refer ?: "",
        videoInfo.imageUrl ?: "",
        videoInfo.videoId,
        ResourceType.ARTICLE,
        videoInfo = videoInfo
    )

    constructor(movieInfo: MovieInfo, videoIndex: Int, position: Long) : this(
        0,
        movieInfo.name ?: "",
        movieInfo.refer ?: "",
        movieInfo.refer ?: "",
        movieInfo.imageUrl ?: "",
        movieInfo.id,
        ResourceType.MOVIE,
        movieInfo = movieInfo,
        videoProgress = position,
        movieProgress = videoIndex,
        modifyTime = System.currentTimeMillis()
    )
}
